package org.bonorsoft.modules.wechat.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.modules.control.entity.House;
import org.bonorsoft.modules.control.entity.Population;
import org.bonorsoft.modules.control.service.IHouseService;
import org.bonorsoft.modules.control.service.IPopulationService;
import org.bonorsoft.modules.control.vo.PopulationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 小程序端实有房屋信息
 */
@Slf4j
@Controller
@RequestMapping("/wechat/house")
public class WxHouseController extends BonorController<House, IHouseService> {

    @Autowired
    private IHouseService houseService;

    @Autowired
    private IPopulationService populationService;


    /**
     * 实有房屋分页列表查询
     *
     * @param house
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "实有房屋信息-分页列表查询")
    @RequiresPermissions("wechat:house:list")
    @GetMapping(value = "/list")
    @ResponseBody
    public Result<?> queryPageList(House house,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<House> queryWrapper = QueryGenerator.initQueryWrapper(house, req.getParameterMap());
        Page<House> page = new Page<House>(pageNo, pageSize);
        IPage<House> pageList = houseService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 实有房屋添加
     *
     * @param house
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> add(@RequestBody House house) {
        house.setInTime(new Date()).setGxsj(new Date());
        houseService.save(house);
        return Result.ok("添加成功！");
    }

    /**
     * 实有房屋详细
     * @param req
     * @return
     */
    @GetMapping("/detail")
    @ResponseBody
    public Result<?> detail(HttpServletRequest req) {
        String id = req.getParameter("id");
        Result<JSONObject> result = new Result<>();
        JSONObject obj = new JSONObject();
        obj.put("detail",houseService.getOne(new QueryWrapper<House>().eq("dzbm", id)));
        result.setResult(obj);
        result.success("查询成功！");
        return result;
    }

    /**
     * 实有房屋详细+实有人口
     * @param req
     * @return
     */
    @GetMapping("/detailShow")
    @ResponseBody
    public Result<?> detailShow(HttpServletRequest req) {
        String id = req.getParameter("id");
        Result<JSONObject> result = new Result<>();
        JSONObject obj = new JSONObject();
        obj.put("house",houseService.getOne(new QueryWrapper<House>().eq("dzbm", id)));
        List<PopulationVo> populations = populationService.listing(new QueryWrapper<Population>().eq("dzbm", id));
        if (populations.size() > 0) obj.put("populations", populations);
        result.setResult(obj);
        result.success("查询成功！");
        return result;
    }

    /**
     * 实有房屋编辑
     *
     * @param house
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> edit(@RequestBody House house) {
        house.setGxsj(new Date());
        houseService.updateById(house);
        return Result.ok("编辑成功!");
    }

}
